<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="客户名称" prop="cusName">
        <el-input
          v-model="queryParams.cusName"
          placeholder="请输入客户名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="订单名称" prop="orderTitle">
        <el-input
          v-model="queryParams.orderTitle"
          placeholder="请输入订单名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item><!--
      <el-form-item label="开票日期" prop="kpDate">
        <el-date-picker clearable
          v-model="queryParams.kpDate"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="请选择开票日期">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="地址" prop="address">
        <el-input
          v-model="queryParams.address"
          placeholder="请输入地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
     <!-- <el-form-item label="票号" prop="pNum">
        <el-input
          v-model="queryParams.pNum"
          placeholder="请输入票号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="货号" prop="goodNum">
        <el-input
          v-model="queryParams.goodNum"
          placeholder="请输入货号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>-->
      <el-form-item label="产品编号" prop="itemCode">
        <el-input
          v-model="queryParams.itemCode"
          placeholder="请输入产品编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="产品名称" prop="itemName">
        <el-input
          v-model="queryParams.itemName"
          placeholder="请输入产品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
    <!--  <el-form-item label="结账状态" prop="jzFlag">
        <el-select v-model="queryParams.jzFlag" placeholder="请选择结账状态" clearable>
          <el-option
            v-for="dict in dict.type.jz_flag"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
        </el-select>
      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:orderinfo:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['business:orderinfo:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:orderinfo:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['business:orderinfo:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="orderinfoList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单编号" align="center" prop="id" />
      <el-table-column label="公司名称" align="center" prop="companyName" />
   <!--   <el-table-column label="客户编号" align="center" prop="cusId" />-->
      <el-table-column label="客户名称" width="150" align="center" prop="cusName" />
      <el-table-column label="订单名称" align="center" prop="orderTitle" />
      <el-table-column label="开票日期" align="center" prop="kpDate" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.kpDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="地址" align="center" prop="address" />
      <el-table-column label="票号" align="center" prop="pNum" />
      <el-table-column label="货号" align="center" prop="goodNum" />
      <el-table-column label="备注" align="center" prop="remark" />
    <!--  <el-table-column label="结账状态" align="center" prop="jzFlag">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.jz_flag" :value="scope.row.jzFlag"/>
        </template>
      </el-table-column>-->
      <el-table-column label="订单类型" align="center" prop="res1" />
      <!-- <el-table-column label="备用字段2" align="center" prop="res2" />
       <el-table-column label="备用字段3" align="center" prop="res3" />-->
      <el-table-column label="操作" width="100" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:orderinfo:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:orderinfo:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改订单信息对话框 -->
    <el-dialog :close-on-click-modal="false" :title="title" :visible.sync="open" width="1200px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="公司名称" prop="companyName">
              <el-input v-model="form.companyName" placeholder="请输入公司名称" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="客户名称" prop="cusName" label-width="100px">
              <el-select @change="cusChange" v-model="form.cusName" clearable filterable placeholder="请选择客户">
                <el-option
                  v-for="item in khList"
                  :key="item.id"
                  :label="item.cusName"
                  :value="{'lable':item.cusName,'value':item.id,'value2':item.companyName,'value3':item.cusAddress}">
<!--                  :value="item.id"-->
                </el-option>
              </el-select>
<!--              <el-input v-model="form.cusName" placeholder="请输入客户名称"/>-->
            </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
        <el-form-item label="订单名称" prop="orderTitle">
          <el-input v-model="form.orderTitle" placeholder="请输入订单名称" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="开票日期" prop="kpDate">
          <el-date-picker clearable
            v-model="form.kpDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择开票日期">
          </el-date-picker>
        </el-form-item>
          </el-col>
        </el-row>

        <el-row>
          <el-col :span="12">
        <el-form-item label="地址" prop="address">
          <el-input v-model="form.address" placeholder="请输入地址" />
        </el-form-item>
          </el-col>
          <el-col :span="12">
        <el-form-item label="票号" prop="pNum">
          <el-input v-model="form.pNum" placeholder="请输入票号" />
        </el-form-item>
          </el-col>
        </el-row>


        <el-row>
          <el-col :span="12">
            <el-form-item label="货号" prop="goodNum">
              <el-input v-model="form.goodNum" placeholder="请输入货号" />
            </el-form-item>
          </el-col>
          <el-col span="12">
          <!--  <el-form-item label="订单类型" prop="res1">
              <el-input v-model="form.remark" placeholder="请输入订单类型" />
            </el-form-item>-->
            <el-form-item label="订单类型" prop="res1">
              <el-select  style="width: 100%" v-model="form.res1">
                <el-option
                  v-for="dict in dict.type.order_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>

          </el-col>
        </el-row>
        <el-row>
          <el-form-item label="备注" prop="remark">
            <el-input v-model="form.remark" placeholder="请输入备注" />
          </el-form-item>
        </el-row>
      </el-form>
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="chooseP"
          >添加物料</el-button>
        </el-col>
      </el-row>
      <el-table :data="seList" >
        <!--            <el-table-column type="selection" width="55" align="center" />-->
        <el-table-column label="物料编号" width="150" align="center" prop="itemCode"  />
        <el-table-column  min-width="200" show-overflow-tooltip label="物料名称" align="center" prop="itemName" />
       <!-- <el-table-column label="价格111" width="150" align="center" prop="price"  />-->
        <el-table-column label="价格"  align="center" prop="res1" >
          <template slot-scope="scope">
            <div style="display:flex">
              <el-input-number style="width: 100%"  controls-position="right" clearable v-model="scope.row.res1" :precision="2"></el-input-number>
            </div>
          </template>
        </el-table-column>
        <el-table-column label="规格型号" width="150" align="center" prop="spec"  />
        <el-table-column label="大类" width="150" align="center" prop="className"  />
        <el-table-column label="单位" width="150" align="center" prop="unit"  />
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="100">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-delete"
              @click="deleteWu(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <el-dialog :close-on-click-modal="false" :before-close="handleClose" :title="chooseTitle" :visible.sync="chooseOpen" width="1200px">
      <el-form :model="queryParams3" ref="queryForm3" :inline="true" v-show="showSearch2" label-width="68px">
        <el-form-item label="物料名称" prop="itemName">
          <el-input
            v-model="queryParams3.itemName"
            placeholder="请输入物料名称"
            clearable
            size="small"
            @keyup.enter.native="handleQuery3"
          />
        </el-form-item>
        <el-form-item label="物料编号" prop="itemCode">
          <el-input
            v-model="queryParams3.itemCode"
            placeholder="请输入物料编号"
            clearable
            size="small"
            @keyup.enter.native="handleQuery3"
          />
        </el-form-item>
        <el-form-item label="大类" prop="classId">
          <el-select style="width: 100%" filterable v-model="queryParams3.classId" placeholder="大类名称" >
            <el-option
              v-for="dict in classOptions"
              :key="dict.id"
              :label="dict.className"
              :value="dict.id"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery3">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery3">重置</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="danger" size="mini" @click="choose">提 交</el-button>
        </el-form-item>
      </el-form>

      <el-table ref="infoRef" :header-cell-class-name="cellClass"  :row-key="getRowKeys" :data="proList" @select="handleSelectionChange3">
        <el-table-column :reserve-selection='true' type="selection" width="55" align="center" />
        <el-table-column label="物料编号" width="150" align="center" prop="itemCode"  />
        <el-table-column  min-width="200" show-overflow-tooltip label="物料名称" align="center" prop="itemName" />
        <el-table-column label="价格" width="150" align="center" prop="price"  />
        <el-table-column label="规格型号" width="150" align="center" prop="spec"  />
        <el-table-column label="大类" width="150" align="center" prop="className"  />
        <el-table-column label="单位" width="150" align="center" prop="unit"  />
      </el-table>
      <pagination
        v-show="total2>0"
        :total="total2"
        :page.sync="queryParams3.pageNum"
        :limit.sync="queryParams3.pageSize"
        @pagination="getList2"
      />
    </el-dialog>
  </div>
</template>

<script>
import { listOrderinfo, getOrderinfo, delOrderinfo, addOrderinfo, updateOrderinfo } from "@/api/business/orderinfo";
import { listJcCusInfo } from "@/api/base/jcCusInfo";
// import { listIteminfo } from "@/api/base/iteminfo";
import { getListClass} from "@/api/base/class";
import { listIteminfo, getIteminfo, delIteminfo, addIteminfo, updateIteminfo } from "@/api/base/iteminfo";

export default {
  name: "Orderinfo",
  dicts: ['jc_iteminfo_unit','jz_flag',"order_type"],
  data() {
    var validatePrice = (rule, value, callback) => {
      var reg = /^(0|[1-9]\d{0,17})(\.\d{1,2})?$/;
      if(!reg.test(value) && value!=undefined && value!=""){
        return callback(new Error('只能输入数值保留两位小数'));
      } else{
        callback();
      }
    };
    var validateMoney = (rule, value, callback) => {
      var reg = /^(0|[1-9]\d{0,17})(\.\d{1,2})?$/;
      if(!reg.test(value) && value!=undefined && value!=""){
        return callback(new Error('只能输入数值保留两位小数'));
      } else{
        callback();
      }
    };
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      ids3:[],
      // 客户列表
      khList:[],
      //大类单位字典
      classOptions: [],
      // 非单个禁用
      single: true,
      single2: true,
      // 非多个禁用
      multiple: true,
      multiple2: true,
      // 显示搜索条件
      showSearch: true,
      showSearch2: true,
      // 总条数
      total: 0,
      total2: 0,
      // 订单信息表格数据
      orderinfoList: [],
      cpList:[],
      seList:[],
      proList:[],
      wuList:[],
      // 弹出层标题
      title: "",
      title2: "",
      chooseTitle:'',
      // 是否显示弹出层
      open: false,
      chooseOpen:false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        companyName: null,
        cusId: null,
        cusName: null,
        orderTitle: null,
        kpDate: null,
        address: null,
        pNum: null,
        goodNum: null,
        itemCode: null,
        itemName: null,
        spec: null,
        unit: null,
        price: null,
        money: null,
        res1: null,
        res2: null,
        res3: null
      },
      queryParams2: {
        pageNum: 1,
        pageSize: 1000,
      },
      // 查询参数
      queryParams3: {
        pageNum: 1,
        pageSize: 10,
        deptId: null,
        itemName: null,
        itemCode: null,
        unit: null,
        baozhuangType: null,
        baozhuangQty: null,
        spec: null,
        model: null,
        tmh: null,
        classId: 2,
        className: null,
        supplyId: null,
        supplyName: null,
        upLine: null,
        downLine: null,
        middleLine: null,
        stopFlag: null,
        cunchudate: null,
        productiondate: null,
      },
      // 表单参数
      form: {
      },
      // 表单校验
      rules: {
        cusName: [
          { required: true, message: "客户不能为空", trigger: "blur" }
        ],
        cusId: [
          { required: true, message: "客户编号不能为空", trigger: "blur" }
        ],
        price: [
          { validator: validatePrice,trigger: "change" }
        ],
        money: [
          { validator: validateMoney,trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
    this.getList2();
    getListClass().then(response => {
      this.classOptions = response.data;
    });
    listJcCusInfo(this.queryParams2).then(response => {
      this.khList = response.rows;
    });
    listIteminfo(this.queryParams2).then(response => {
      console.log(response,'response')
      this.cpList = response.rows;
    });
  },
  methods: {
    cusChange(value){
      this.form.cusName = value.lable
      this.form.cusId = value.value
      this.form.companyName =value.value2
      this.form.address =value.value3
      console.log(this.form,'this.form')
    },
    cpChange(value){
      this.form.itemName = value.lable
      this.form.itemCode = value.value
    },
    /** 查询订单信息列表 */
    getList() {
      this.loading = true;
      listOrderinfo(this.queryParams).then(response => {
        this.orderinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    /** 查询物料信息列表 */
    getList2() {
      // this.loading = true;
      listIteminfo(this.queryParams3).then(response => {
        console.log(response,'ressssssssss')
        this.proList = response.rows;
        this.total2 = response.total;
        // this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        companyName: null,
        cusId: null,
        cusName: null,
        orderTitle: null,
        kpDate: new Date(),
        address: null,
        pNum: null,
        goodNum: null,
        itemCode: null,
        itemName: null,
        spec: null,
        unit: null,
        price: null,
        money: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        res1: null,
        res2: null,
        res3: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.seList=[],
      this.reset();
      this.open = true;
      this.title = "添加订单信息";
    },
    /** 修改按钮操作 */
      handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getOrderinfo(id).then(response => {
        this.seList=response.data.orderIteminfoList;
        this.form = response.data;
        this.open = true;
        this.title = "修改订单信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            this.form.orderIteminfoList = this.seList
            updateOrderinfo(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            this.form.orderIteminfoList = this.seList
            addOrderinfo(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除订单信息编号为"' + ids + '"的数据项？').then(function() {
        return delOrderinfo(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('business/orderinfo/export', {
        ...this.queryParams
      }, `orderinfo_${new Date().getTime()}.xlsx`)
    },
    chooseP(){
      // this.reset2();
      this.open=false;
      this.chooseOpen = true;
      this.chooseTitle = "选择产品";
      this.getList2();
    },
    handleClose(){
      this.seList = this.wuList
      this.chooseOpen = false
      this.open = true
    },
    /** 搜索按钮操作 */
    handleQuery3() {
      this.queryParams3.pageNum = 1;
      this.getList2();
    },
    /** 重置按钮操作 */
    resetQuery3() {
      this.resetForm("queryForm3");
      this.handleQuery3();
    },
    choose(){
      this.chooseOpen = false
      this.open = true
    },
    cellClass(row){
      if (row.columnIndex === 0) {
        return 'DisableSelection'
      }
    },
    getRowKeys(row) {
      //记录每行的key值
      return row.id;
    },
    handleSelectionChange3(selection){
      // let selected = selection.length && selection.indexOf(row) !==-1

      this.ids3 = selection.map(item => item.id)
      this.single2 = selection.length!==1
      this.multiple2 = !selection.length
      this.wuList = selection
      this.seList = this.wuList
    },
    deleteWu(row){
      this.seList = this.seList.filter((item)=>{
        return item.id !== row.id
      })
    },
  }
};
</script>
<style lang="scss" scoped>
  // 隐藏全选框
  ::v-deep .DisableSelection > .cell {
    display: none !important;
  }
</style>
